﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ColorRichTextBox.ColorRichTextBox;
using Ionic.Zip;
using System.IO;
using GAM_Web_Designer_Pro.Properties;
using System.Runtime.Serialization.Formatters.Binary;

namespace GAM_Web_Designer_Pro.Forms {
    public partial class NewProjectTemplateForm : Form {

        private SortedList<string, clsColorRichTextBoxSchema> _schemas;

        public NewProjectTemplateForm() {
            InitializeComponent();
            _schemas = SchemaAccess.LoadAllSchemas();
        }

        private void browserButton_Click( object sender, EventArgs e ) {
            if ( folderBrowserDialog1.ShowDialog( this ) == DialogResult.OK )
                _dirTB.Text = folderBrowserDialog1.SelectedPath;
        }

        private void createButton_Click( object sender, EventArgs e ) {
            if ( VerifyInfo() ) {
                try {
                    
                    string dataDir = Application.StartupPath + Settings.Default.ProjectTemplateDir;

                    ProjectTemplate temp = new ProjectTemplate();
                    temp.Name = _nameTB.Text.Trim();
                    temp.Language = _langCB.Text.Trim();
                    temp.ZipFile = dataDir + @"\" + temp.Name + ".zip";

                    ZipFile zip = new ZipFile( temp.ZipFile );
                    zip.Password = Settings.Default.ProjectZipPass;
                    zip.Encryption = EncryptionAlgorithm.WinZipAes128;
                    zip.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression;
                    zip.AddDirectory( _dirTB.Text.Trim() );
                    zip.Save();

                    FileStream stream = File.Create( dataDir + @"\" + temp.Name + Settings.Default.ProjTempExt );
                    BinaryFormatter formatter = new BinaryFormatter();
                    formatter.Serialize( stream, temp );
                    stream.Close();

                    MessageBox.Show( this, "Project Template Created!", "", MessageBoxButtons.OK, MessageBoxIcon.Information );
                    this.DialogResult = DialogResult.OK;
                    this.Close();

                }
                catch ( Exception ex ) {
                    new BugReporter.BugReporterForm( Application.ProductName,
                      Application.ProductVersion, ex.ToString(), ex.Message ).Show();
                }
            }
        }

        private void cancelButton_Click( object sender, EventArgs e ) {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void NewProjectTemplateForm_Load( object sender, EventArgs e ) {
            foreach ( clsColorRichTextBoxSchema item in _schemas.Values ) {
                if ( item.SchemeType != "IMAGE" )
                    _langCB.Items.Add( item.SchemeType );
            }
        }

        private bool VerifyInfo() {
            if ( _nameTB.Text.Trim().Equals( string.Empty ) ) {
                MessageBox.Show( this, "The Project Template Name Textbox can't be left empty", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation );
                return false;
            }
            else if ( _langCB.Text.Trim().Equals( string.Empty ) ) {
                MessageBox.Show( this, "You must select a progamming language. If the template " +
                    "is for just text select Other.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation );
                return false;
            }
            else if ( _dirTB.Text.Trim().Equals( string.Empty ) ) {
                MessageBox.Show( this, "The Project Template Directory Textbox can't be left empty", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation );
                return false;
            }
            else
                return true;
        }
    }
}
